package com.milhouz.quizzdroid.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class QuizzDroidUserProvider extends ContentProvider {

	private QuizzDroidUserOpenHelper mOpenHelper;

	private SQLiteDatabase mQuizzDroidsDB;

	public static final String PROVIDER_NAME = "com.milhouz.quizzdroid.db.QuizzDroidUserProvider";
	public static final Uri USERS_URI = Uri.parse("content://" + PROVIDER_NAME + "/users");

	private static final int USERS = 1;
	private static final int USER_NAME = 2;

	private static final UriMatcher URI_MATCHER;
	static {
		URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URI_MATCHER.addURI(PROVIDER_NAME, "users", USERS);
		URI_MATCHER.addURI(PROVIDER_NAME, "users/#", USER_NAME);
	}

	@Override
	public boolean onCreate() {
		mOpenHelper = new QuizzDroidUserOpenHelper(getContext());
		mQuizzDroidsDB = mOpenHelper.getWritableDatabase();
		return (mQuizzDroidsDB == null) ? false : true;
	}

	@Override
	public String getType(Uri uri) {
		switch (URI_MATCHER.match(uri)) {
		case USERS:
			return "vnd.android.cursor.dir/vnd.quizzdroid.users";
		case USER_NAME:
			return "vnd.android.cursor.item/vnd.quizzdroid.user";
		default:
			throw new IllegalArgumentException("Unsupported URI: " + uri);
		}
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
		// Choose the table to request following the URI
		sqlBuilder.setTables(QuizzDroidConstants.TABLE_NAME_USERS);
		// Choose the conditions request following the URI
		if (URI_MATCHER.match(uri) == USER_NAME) {
			// Querying a particular question
			sqlBuilder.appendWhere(QuizzDroidConstants.ID + " = " + uri.getPathSegments().get(1));
		}
		Cursor c = sqlBuilder.query(mQuizzDroidsDB //
				, projection //
				, selection //
				, selectionArgs //
				, null //
				, null //
				, sortOrder);

		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long rowID = 0;
		Uri uriWithId = null;
		switch (URI_MATCHER.match(uri)) {
		case USERS:
			rowID = mQuizzDroidsDB.insert(QuizzDroidConstants.TABLE_NAME_USERS, "", values);
			if (rowID > 0) { // if added successfully
				uriWithId = ContentUris.withAppendedId(USERS_URI, rowID);
			}
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		if (uriWithId != null) {
			getContext().getContentResolver().notifyChange(uriWithId, null);
			return uriWithId;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public int update(Uri uri, ContentValues values, String arg2, String[] arg3) {
		int rowID = 0;
		switch (URI_MATCHER.match(uri)) {
		case USER_NAME:
			rowID = mQuizzDroidsDB.update(QuizzDroidConstants.TABLE_NAME_USERS, values, QuizzDroidConstants.ID + "=" + uri.getLastPathSegment(), null);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		if (rowID != 0) {
			getContext().getContentResolver().notifyChange(uri, null);
			return rowID;
		}
		throw new SQLException("Failed to update row into " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int count = 0;
		switch (URI_MATCHER.match(uri)) {
		case USERS:
			count = mQuizzDroidsDB.delete(QuizzDroidConstants.TABLE_NAME_USERS, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}
